function varargout = memc_mergin(varargin)
% MEMC_ADD M-file for memc_mergin.fig
%      MEMC_ADD, by itself, creates a new MEMC_ADD or raises the existing
%      singleton*.
%
%      H = MEMC_MERGIN returns the handle to a new MEMC_MERGIN or the handle to
%      the existing singleton*.
%
%      MEMC_MERGIN('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MEMC_MERGIN.M with the given input arguments.
%
%      MEMC_MERGIN('Property','Value',...) creates a new MEMC_MERGIN or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before memc_add_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to memc_add_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help memc_mergin

% Last Modified by GUIDE v2.5 28-Jul-2010 18:05:42

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @memc_add_OpeningFcn, ...
                   'gui_OutputFcn',  @memc_add_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before memc_mergin is made visible.
function memc_add_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to memc_mergin (see VARARGIN)
global IsPlayback;
IsPlayback = 0;

handles.logo_size    = [];
handles.logo_cdata   = [];
handles.stream_size  = [];
handles.stream_cdata = [];
handles.stream_frames_max  = 0;
handles.stream_frames_cur = 0;
handles.stream_name = '';
handles.logo_pos = [];
handles.stream_pos = [];
handles.logo_mask = [];
handles.scale = 1;
handles.isMergin   = 0;
handles.mergin_action = 0;
handles.stratFrame = 1;
handles.endFrame = 1;
handles.startFrameOut = 1;
handles.endFrameOut = 1;

set(handles.tagStartFrameEdit,'String',sprintf('%g',handles.stratFrame));
set(handles.tagEndFrameEdit,'String',sprintf('%g',handles.endFrame));
set(handles.tagStreamStartFrameEdit,'String',sprintf('%g',handles.startFrameOut));
set(handles.tagStreamEndFrameEdit,'String',sprintf('%g',handles.endFrameOut));
set(handles.tagScaleEdit,'String',sprintf('%g',handles.scale));

handles.codec_list = mmwrite('','ListAviVideoEncoders');
handles.codec_id   = 1;
set(handles.tagCodecPopupmenu,'String',handles.codec_list,'Value',handles.codec_id);

% Choose default command line output for memc_mergin
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes memc_mergin wait for user response (see UIRESUME)
% uiwait(handles.tagMainFigure);


% --- Outputs from this function are returned to the command line.
function varargout = memc_add_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in tagOpenStreamButton.
function tagOpenStreamButton_Callback(hObject, eventdata, handles)
% hObject    handle to tagOpenStreamButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[szFileName, szPathName] = uigetfile({'*.mpg','MPG files (*.mpg)';'*.avi','AVI files (*.avi)';'*.*','ALL files (*.*)'},'Open a Sequence Video...');
if ~isequal(szPathName,0)
      handles.stream_name = fullfile(szPathName,szFileName);
      tmpVideo  = mmread(handles.stream_name,-1);
      if(tmpVideo.nrFramesTotal < 0)
          tmpVideo.nrFramesTotal = floor(0.95 * (-tmpVideo.nrFramesTotal));
      end
      handles.stream_frames_max  = tmpVideo.nrFramesTotal;
      handles.stream_frames_cur = 1;
      tmpVideo = mmread(handles.stream_name,handles.stream_frames_cur:handles.stream_frames_cur);
      handles.stream_cdata = tmpVideo.frames.cdata;
      tmpSize = size(handles.stream_cdata);
      handles.stream_size = [tmpSize(2),tmpSize(1)];   
      handles.mergin_cdata = MerginCDATA(handles,handles.stream_cdata);
      imshow(handles.mergin_cdata,'Parent',handles.tagStreamAxes);
      tmpStr = sprintf('Stream View: Size%gx%g, Frames:%g/%g',handles.stream_size(1),handles.stream_size(2),handles.stream_frames_cur,handles.stream_frames_max);
      set(handles.tagStreamUipanel,'Title',tmpStr);
      handles.startFrame = 1;
      handles.endFrame   = handles.stream_frames_max;
      handles.startFrameOut = 1;
      handles.endFrameOut   = handles.stream_frames_max;
      set(handles.tagStartFrameEdit,'String',sprintf('%g',handles.startFrame));
      set(handles.tagEndFrameEdit,'String',sprintf('%g',handles.endFrame));
      set(handles.tagStreamStartFrameEdit,'String',sprintf('%g',handles.startFrameOut));
      set(handles.tagStreamEndFrameEdit,'String',sprintf('%g',handles.endFrameOut));
end
guidata(hObject, handles);

% --- Executes on button press in tagOpenLogoButton.
function tagOpenLogoButton_Callback(hObject, eventdata, handles)
% hObject    handle to tagOpenLogoButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[szFileName, szPathName] = uigetfile({'*.jpg','JPG files (*.jpg)';'*.bmp','BMP files (*.bmp)';'*.*','ALL files (*.*)'},'Open a Logo files');
if ~isequal(szPathName,0)
    handles.logo_cdata = imread(fullfile(szPathName,szFileName));
    imshow(handles.logo_cdata,'Parent',handles.tagLogoAxes);
    tmpSize = size(handles.logo_cdata);
    handles.logo_size = [tmpSize(2),tmpSize(1)];
    tmpStr = sprintf('Logo View: Size%gx%g',handles.logo_size(1),handles.logo_size(2));
    set(handles.tagLogoUipanel,'Title',tmpStr);
    set(handles.tagColorEdit,'String','');
    handles.logo_mask  = [];
end
guidata(hObject, handles);


% --- Executes on button press in tagMerginCheck.
function tagMerginCheck_Callback(hObject, eventdata, handles)
% hObject    handle to tagMerginCheck (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of tagMerginCheck
global IsPlayback;
tmp = get(hObject,'Value');
if tmp ~= handles.isMergin
   handles.isMergin = tmp;
   handles.logo_pos = [];
   handles.stream_pos = [];
   if IsPlayback == 0
      handles.mergin_cdata = MerginCDATA(handles,handles.stream_cdata);
      imshow(handles.mergin_cdata,'Parent',handles.tagStreamAxes);
   end
end   
guidata(hObject, handles);

% --- Executes on button press in tagPlayButton.
function tagPlayButton_Callback(hObject, eventdata, handles)
% hObject    handle to tagPlayButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global IsPlayback;
if IsPlayback == 1
   IsPlayback = 0;
   set(handles.tagPlayButton,'String','Play');
   return;
else
   IsPlayback = 1;
   set(handles.tagPlayButton,'String','Stop');
end

if handles.stream_frames_cur == handles.stream_frames_max
   handles.stream_frames_cur = 1;
else
   handles.stream_frames_cur = handles.stream_frames_cur + 1;
end
guidata(handles.tagMainFigure,handles);

while IsPlayback == 1 
  handles = guidata(handles.tagMainFigure);
  tmpVideo = mmread(handles.stream_name,handles.stream_frames_cur:handles.stream_frames_cur,[],false,true,'',false,true);
  handles.stream_cdata = tmpVideo.frames.cdata;
  handles.mergin_cdata = MerginCDATA(handles,handles.stream_cdata);
  imshow(handles.mergin_cdata,'Parent',handles.tagStreamAxes);
  tmpStr = sprintf('Stream View: Size%gx%g, Frames:%g/%g',handles.stream_size(1),handles.stream_size(2),handles.stream_frames_cur,handles.stream_frames_max);
  set(handles.tagStreamUipanel,'Title',tmpStr);
  pause(0.05);
  if IsPlayback ==0 || handles.stream_frames_cur == handles.stream_frames_max
      break;
  end
  handles.stream_frames_cur = handles.stream_frames_cur + 1;
  guidata(handles.tagMainFigure,handles);
end
IsPlayback = 0;
set(handles.tagPlayButton,'String','Play');
guidata(handles.tagMainFigure,handles);


% --- Executes on button press in tagSaveButton.
function tagSaveButton_Callback(hObject, eventdata, handles)
% hObject    handle to tagSaveButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename,pathname]= uiputfile({'*.*','All Files(*.*)'},'Save Stream As...');
if isequal([filename pathname],[0,0])
   return;
end
handles.outfile_name = fullfile(pathname,filename); 

conf.videoCompressor = handles.codec_list(handles.codec_id);
conf.outputFrameRate = 30;

set(handles.tagMainFigure,'Pointer','fleur');
set(handles.activex1,'min',handles.startFrameOut-1,'max',handles.endFrameOut);
set(handles.activex1,'portionvalue', handles.startFrameOut-1,'enable',true);

for curIndex = handles.startFrameOut:handles.endFrameOut
  video_pre = mmread(handles.stream_name,curIndex,[],false,true,'',false,true);
  pause(0.05);
  if curIndex >= handles.startFrame && curIndex <= handles.endFrame
     video_pre.frames.cdata = MerginCDATA(handles,video_pre.frames.cdata);
  end 
  if curIndex == handles.endFrameOut
     mmwrite(handles.outfile_name, video_pre, conf,'Initialized'); 
  elseif curIndex == handles.startFrameOut
     mmwrite(handles.outfile_name, video_pre, conf, 'Continue'); 
  else
     mmwrite(handles.outfile_name, video_pre, conf,'Continue','Initialized');
  end
  set(handles.activex1, 'portionvalue', curIndex);
end
set(handles.tagMainFigure,'Pointer','arrow');


function tagColorEdit_Callback(hObject, eventdata, handles)
% hObject    handle to tagColorEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of tagColorEdit as text
%        str2double(get(hObject,'String')) returns contents of tagColorEdit as a double


% --- Executes during object creation, after setting all properties.
function tagColorEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to tagColorEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function tagMainFigure_WindowButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to tagMainFigure (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
pt = get(handles.tagLogoAxes,'CurrentPoint');
pic_x = round(pt(1,1));
pic_y = round(pt(1,2));
if pic_x < 1 ||  pic_y < 1 || isempty(handles.logo_size)
   return;
end
if pic_x > handles.logo_size(1) || pic_y > handles.logo_size(2)
   return;
end

if ~isempty(handles.logo_cdata)
  handles.logo_pos = [pic_y,pic_x];
  if handles.isMergin==1
     handles.mergin_action = 1;
  else
     tmpColor = handles.logo_cdata(pic_y,pic_x,:);
     tmpStr = sprintf(' [%g,%g,%g]',tmpColor(1),tmpColor(2),tmpColor(3));
     set(handles.tagColorEdit,'String',tmpStr);
     tmp_1 = handles.logo_cdata(:,:,1) == tmpColor(1);
     tmp_2 = handles.logo_cdata(:,:,2) == tmpColor(2);
     tmp_3 = handles.logo_cdata(:,:,3) == tmpColor(3);
     handles.logo_mask  = double(~(tmp_1 & tmp_2 & tmp_3));
  end
else
  handles.logo_pos = [];
  handles.logo_mask = [];
end
guidata(hObject, handles);


% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function tagMainFigure_WindowButtonUpFcn(hObject, eventdata, handles)
% hObject    handle to tagMainFigure (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
pt = get(handles.tagStreamAxes,'CurrentPoint');
pic_x = round(pt(1,1));
pic_y = round(pt(1,2));
if isempty(handles.stream_size) || isempty(handles.logo_size) || handles.isMergin == 0 || handles.mergin_action ==0
   handles.mergin_action = 0;
   guidata(hObject, handles);
   return;
end
if pic_x > handles.stream_size(1) || pic_y > handles.stream_size(2) || pic_x < -handles.logo_size(1) || pic_y < -handles.logo_size(2)
   handles.mergin_action = 0;
   guidata(hObject, handles);
   return;
end

if ~isempty(handles.stream_cdata) && ~isempty(handles.logo_cdata)
  handles.stream_pos = [pic_y,pic_x];
  handles.mergin_cdata = MerginCDATA(handles,handles.stream_cdata);
  imshow(handles.mergin_cdata,'Parent',handles.tagStreamAxes);
  handles.mergin_action = 0;
  guidata(hObject, handles);
end


% --- Executes on mouse motion over figure - except title and menu.
function tagMainFigure_WindowButtonMotionFcn(hObject, eventdata, handles)
% hObject    handle to tagMainFigure (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
pt = get(handles.tagStreamAxes,'CurrentPoint');
pic_x = round(pt(1,1));
pic_y = round(pt(1,2));
if isempty(handles.stream_size) || isempty(handles.logo_size) || handles.isMergin == 0 || handles.mergin_action ==0
   return;
end
if pic_x > handles.stream_size(1) || pic_y > handles.stream_size(2) || pic_x < -handles.logo_size(1) || pic_y < -handles.logo_size(2)
   return;
end

if ~isempty(handles.stream_cdata) && ~isempty(handles.logo_cdata)
  handles.stream_pos = [pic_y,pic_x];
  handles.mergin_cdata = MerginCDATA(handles,handles.stream_cdata);
  imshow(handles.mergin_cdata,'Parent',handles.tagStreamAxes);
  guidata(hObject, handles);
end

%--------------------------------------------------------------------------
function mergin_cdata = MerginCDATA(handles,stream_cdata)
if handles.isMergin == 1 && ~isempty(handles.logo_mask) && ~isempty(handles.stream_pos) && ~isempty(handles.logo_pos)
   YOffset0 = handles.stream_pos(1)-handles.logo_pos(1)+1;
   XOffset0 = handles.stream_pos(2)-handles.logo_pos(2)+1;
   YOffset1 = YOffset0 + handles.logo_size(2) -1;
   XOffset1 = XOffset0 + handles.logo_size(1) -1;
   logo_xoffset0 = 1;
   logo_xoffset1 = handles.logo_size(1);
   logo_yoffset0 = 1;
   logo_yoffset1 = handles.logo_size(2);
   if YOffset0 < 1
      logo_yoffset0 = logo_yoffset0 + 1-YOffset0;
      YOffset0 = 1;
   end
   if YOffset1 > handles.stream_size(2)
      logo_yoffset1 = logo_yoffset1 + handles.stream_size(2)-YOffset1;
      YOffset1 = handles.stream_size(2);
   end
   if XOffset0 < 1
      logo_xoffset0 = logo_xoffset0 + 1-XOffset0;
      XOffset0 = 1;
   end
   if XOffset1 > handles.stream_size(1)
      logo_xoffset1 = logo_xoffset1 + handles.stream_size(1)-XOffset1; 
      XOffset1 = handles.stream_size(1);
   end
   mergin_cdata = double(stream_cdata);
   LogoScale   =  handles.scale * handles.logo_mask(logo_yoffset0:logo_yoffset1,logo_xoffset0:logo_xoffset1);
   StreamScale =  ones(logo_yoffset1-logo_yoffset0+1,logo_xoffset1-logo_xoffset0+1) - LogoScale;

   mergin_cdata(YOffset0:YOffset1,XOffset0:XOffset1,1) = StreamScale.* mergin_cdata(YOffset0:YOffset1,XOffset0:XOffset1,1)+...
                                                         LogoScale.*double(handles.logo_cdata(logo_yoffset0:logo_yoffset1,logo_xoffset0:logo_xoffset1,1));
   mergin_cdata(YOffset0:YOffset1,XOffset0:XOffset1,2) = StreamScale.* mergin_cdata(YOffset0:YOffset1,XOffset0:XOffset1,2)+...
                                                         LogoScale.*double(handles.logo_cdata(logo_yoffset0:logo_yoffset1,logo_xoffset0:logo_xoffset1,2));
   mergin_cdata(YOffset0:YOffset1,XOffset0:XOffset1,3) = StreamScale.* mergin_cdata(YOffset0:YOffset1,XOffset0:XOffset1,3)+...
                                                         LogoScale.*double(handles.logo_cdata(logo_yoffset0:logo_yoffset1,logo_xoffset0:logo_xoffset1,3));
   mergin_cdata= uint8(mergin_cdata);
else
   mergin_cdata = stream_cdata;
end

function tagScaleEdit_Callback(hObject, eventdata, handles)
% hObject    handle to tagScaleEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of tagScaleEdit as text
%        str2double(get(hObject,'String')) returns contents of tagScaleEdit as a double
tmpScale = str2double(get(hObject,'String'));
if tmpScale < 0 || tmpScale > 1;
   if tmpScale < 0
      tmpScale = 0.0;
   else
      tmpScale = 1.0;
   end
   set(hObject,'String',sprintf('%g',tmpScale));
end
handles.scale = tmpScale;
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function tagScaleEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to tagScaleEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in tagCodecPopupmenu.
function tagCodecPopupmenu_Callback(hObject, eventdata, handles)
% hObject    handle to tagCodecPopupmenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns tagCodecPopupmenu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from tagCodecPopupmenu
handles.codec_id = get(hObject, 'value'); 
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function tagCodecPopupmenu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to tagCodecPopupmenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end



function tagStartFrameEdit_Callback(hObject, eventdata, handles)
% hObject    handle to tagStartFrameEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of tagStartFrameEdit as text
%        str2double(get(hObject,'String')) returns contents of tagStartFrameEdit as a double
tmp = str2double(get(hObject,'String'));
if tmp < 1 || tmp > handles.stream_frames_max
    set(hObject,'String',sprintf('%g',handles.startFrame));
    return;
else
    handles.startFrame = tmp;
end
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function tagStartFrameEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to tagStartFrameEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function tagEndFrameEdit_Callback(hObject, eventdata, handles)
% hObject    handle to tagEndFrameEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of tagEndFrameEdit as text
%        str2double(get(hObject,'String')) returns contents of tagEndFrameEdit as a double
tmp = str2double(get(hObject,'String'));
if tmp < 1 || tmp > handles.stream_frames_max
    set(hObject,'String',sprintf('%g',handles.endFrame));
    return;
elseif tmp < handles.startFrame
    handles.endFrame = handles.startFrame;
    set(hObject,'String',sprintf('%g',handles.endFrame));
    return;
else    
    handles.endFrame = tmp;
end
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function tagEndFrameEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to tagEndFrameEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function tagStreamStartFrameEdit_Callback(hObject, eventdata, handles)
% hObject    handle to tagStreamStartFrameEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of tagStreamStartFrameEdit as text
%        str2double(get(hObject,'String')) returns contents of tagStreamStartFrameEdit as a double
tmp = str2double(get(hObject,'String'));
if tmp < 1 || tmp > handles.stream_frames_max
    set(hObject,'String',sprintf('%g',handles.startFrameOut));
    return;
else
    handles.startFrameOut = tmp;
end
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function tagStreamStartFrameEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to tagStreamStartFrameEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function tagStreamEndFrameEdit_Callback(hObject, eventdata, handles)
% hObject    handle to tagStreamEndFrameEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of tagStreamEndFrameEdit as text
%        str2double(get(hObject,'String')) returns contents of tagStreamEndFrameEdit as a double
tmp = str2double(get(hObject,'String'));
if tmp < 1 || tmp > handles.stream_frames_max
    set(hObject,'String',sprintf('%g',handles.endFrameOut));
    return;
elseif tmp < handles.startFrameOut
    handles.endFrame = handles.startFrameOut;
    set(hObject,'String',sprintf('%g',handles.endFrameOut));
    return;
else    
    handles.endFrameOut = tmp;
end
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function tagStreamEndFrameEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to tagStreamEndFrameEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


